//
//  UnarmedDetailView.swift
//  SBSafetyApp
//
//  Created by Lina on 2022/12/30.
//

import Foundation
import UIKit

class UnarmedDetailView: UIScrollView {
    let basicBV = UnarmedDetailBasicView()
   
    let recordBV = UIView.createBase()
    let stackView = UIStackView()

    var records: [PatrolRecordModal?] = []

    override init(frame: CGRect) {
        super.init(frame: frame)
        showsVerticalScrollIndicator = false
        setupUI()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func updateUI(withModal modal: AlarmCbfinfoModal?) {
        basicBV.updateUI(withModal: modal)
        
        stackView.removeAllSubViews()        
        let devices = modal?.deviceList ?? []
        for device in devices {
            let view = UnarmedDetailRecordView()
            view.updateUI(withModal: device)
            stackView.addArrangedSubview(view)
            view.snp.makeConstraints { make in
                make.height.equalTo(98)
                make.left.right.equalToSuperview()
            }
        }
    }
    
    func setupUI() {
        // 基本信息
        addSubview(basicBV)
        basicBV.snp.makeConstraints { make in
            make.top.equalTo(self.snp.top).offset(10)
            make.centerX.equalToSuperview()
            make.width.equalTo(ScreenWidth - 20)
        }
        
        // 巡检记录
        addSubview(recordBV)
        recordBV.snp.makeConstraints { make in
            make.top.equalTo(basicBV.snp.bottom).offset(10)
            make.centerX.equalToSuperview()
            make.width.equalTo(ScreenWidth - 20)
            make.bottom.equalToSuperview()
        }
        
        let recordTitleL = UILabel()
        recordTitleL.text = "———  主机信息列表  ———"
        recordTitleL.textAlignment = .center
        recordTitleL.textColor = .hex("#0F499E")
        recordTitleL.font = .systemFont(ofSize: 17)
        recordBV.addSubview(recordTitleL)
        recordTitleL.snp.makeConstraints { make in
            make.top.equalTo(recordBV.snp.top).offset(20)
            make.centerX.equalToSuperview()
            make.width.equalTo(ScreenWidth - 20)
        }
        
        stackView.alignment = .fill
        stackView.axis = .vertical
        stackView.distribution = .fillEqually
        stackView.spacing = 10.0
        recordBV.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.top.equalTo(recordTitleL.snp.bottom).offset(20)
            make.bottom.equalToSuperview().offset(-20)
        }
    }
}

// MARK: - UnarmedDetailBasicView
class UnarmedDetailBasicView: UIView {
    let statusItem = InspListDetailItem()
//    let netItem = InspListDetailItem()
    let branchItem = InspListDetailItem()
    let cycleItem = InspListDetailItem()
    let addrItem = InspListDetailItem()
    let endItem = InspListDetailItem()
    let sponsorItem = InspListDetailItem()
    let liableItem = InspListDetailItem()
    let unArmedItem = InspListDetailItem()
//    let workDItem = InspListDetailItem()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        layer.cornerRadius = 10
        layer.masksToBounds = true
        backgroundColor = .white
        setupUI()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func updateUI(withModal modal: AlarmCbfinfoModal?) {
        statusItem.update(withKey: "布防状态:", value: modal?.rwzq == "1" ? "已布防" : "未布防")
//        netItem.update(withKey: "类别:", value: label ?? "--")
        branchItem.update(withKey: "所属机构:", value: modal?.wdfj ?? "--")
        cycleItem.update(withKey: "机构名称:", value: modal?.wdmc ?? "--")
        addrItem.update(withKey: "地址:", value: modal?.wddz ?? "--")
        endItem.update(withKey: "场所负责人:", value: modal?.wdzrr ?? "--")
        sponsorItem.update(withKey: "安保负责人:", value: modal?.lzrmc ?? "--")
        liableItem.update(withKey: "保安公司:", value: modal?.bagsmc ?? "--")
        unArmedItem.update(withKey: "实际撤防时间:", value: modal?.tjsj ?? "--")
//        workDItem.update(withKey: "工作日:", value: "--")
    }
    
    func setupUI() {
        addSubview(statusItem)
        statusItem.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(46)
        }

//        addSubview(netItem)
//        netItem.snp.makeConstraints { make in
//            make.left.right.equalToSuperview()
//            make.top.equalTo(statusItem.snp.bottom)
//            make.height.equalTo(statusItem.snp.height)
//        }
        
        addSubview(branchItem)
        branchItem.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(statusItem.snp.bottom)
            make.height.equalTo(statusItem.snp.height)
        }
        
        addSubview(cycleItem)
        cycleItem.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(branchItem.snp.bottom)
            make.height.equalTo(statusItem.snp.height)
        }
        
        addSubview(addrItem)
        addrItem.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(cycleItem.snp.bottom)
            make.height.equalTo(statusItem.snp.height)
        }
        
        addSubview(endItem)
        endItem.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(addrItem.snp.bottom)
            make.height.equalTo(statusItem.snp.height)
        }
        
        addSubview(sponsorItem)
        sponsorItem.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(endItem.snp.bottom)
            make.height.equalTo(statusItem.snp.height)
        }
        
        addSubview(liableItem)
        liableItem.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(sponsorItem.snp.bottom)
            make.height.equalTo(statusItem.snp.height)
        }
        
        addSubview(unArmedItem)
        unArmedItem.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(liableItem.snp.bottom)
            make.height.equalTo(statusItem.snp.height)
            make.bottom.equalToSuperview().offset(-10)
        }
        
//        addSubview(workDItem)
//        workDItem.snp.makeConstraints { make in
//            make.left.right.equalToSuperview()
//            make.top.equalTo(unArmedItem.snp.bottom)
//            make.height.equalTo(statusItem.snp.height)
//            make.bottom.equalToSuperview().offset(-10)
//        }
    }
}

class UnarmedDetailRecordView: UIView {
    let ic = UIImageView(image: UIImage(named: "ic_unarm_error"))
    let numL = UILabel()
    let titleL = UILabel()
    let dateL = UILabel()
    let statusL = UILabel()

    override init(frame: CGRect) {
        super.init(frame: frame)
        layer.cornerRadius = 8
        layer.masksToBounds = true
        backgroundColor = .hex("#F6F8FB")
        setupUI()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func updateUI(withModal modal: AlarmCbfinfoDeviceModal) {
        numL.text = modal.tbDeviceName
//        titleL.text = "\(modal.tbDevicePosition ?? "")（\(modal.tbWebsiteName ?? "")）"
        titleL.text = modal.tbDevicePosition
        dateL.text = modal.tbAlarmTime
        if modal.tbAlarmType == "5" {
            ic.image = UIImage(named: "ic_unarm_error")
            statusL.text = "已撤防"
            statusL.textColor = .hex("#EA0001")
        }
        if modal.tbAlarmType == "6" {
            ic.image = UIImage(named: "ic_unarm_success")
            statusL.text = "已布防"
            statusL.textColor = .hex("#008B27")
        }
    }

    func setupUI() {
        addSubview(ic)
        ic.snp.makeConstraints { make in
            make.left.equalTo(self.snp.left).offset(18)
            make.top.equalTo(self.snp.top).offset(18)
            make.width.height.equalTo(17)
        }
        
        numL.textColor = .hex("#414A57")
        numL.font = .systemFont(ofSize: 14)
        addSubview(numL)
        numL.snp.makeConstraints { make in
            make.left.equalTo(ic.snp.right).offset(6)
            make.centerY.equalTo(ic.snp.centerY)
        }
        
        let ic2 = UIImageView(image: UIImage(named: "ic_unarm_star"))
        addSubview(ic2)
        ic2.snp.makeConstraints { make in
            make.left.equalTo(numL.snp.left)
            make.top.equalTo(numL.snp.bottom).offset(8)
            make.width.height.equalTo(17)
        }
        
        titleL.textColor = .hex("#414A57")
        titleL.font = .systemFont(ofSize: 14)
        addSubview(titleL)
        titleL.snp.makeConstraints { make in
            make.left.equalTo(ic2.snp.right).offset(6)
            make.centerY.equalTo(ic2.snp.centerY)
        }
        
        dateL.textColor = .hex("#414A57")
        dateL.font = .systemFont(ofSize: 14)
        addSubview(dateL)
        dateL.snp.makeConstraints { make in
            make.left.equalTo(titleL.snp.left)
            make.top.equalTo(titleL.snp.bottom).offset(6)
        }
        
        statusL.textColor = .hex("#0F499E")
        statusL.font = .systemFont(ofSize: 14)
        addSubview(statusL)
        statusL.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-17)
            make.centerY.equalTo(ic.snp.centerY)
        }
    }
}
